package com.yex.modules.etcc.controller;

import com.yex.common.utils.DateUtils;
import com.yex.common.utils.PageUtils;
import com.yex.common.utils.R;
import com.yex.common.validator.ValidatorUtils;
import com.yex.entity.InvestmentEntity;
import com.yex.service.InvestmentOrderService;
import com.yex.service.InvestmentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * @author Little Liu
 * @version V1
 * @date Created in 2018/12/27 16:50
 * @description 描述
 */
@RestController
@RequestMapping("/etcc")
public class InvestmentController {

    @Autowired
    private InvestmentService investmentService;
    @Autowired
    private InvestmentOrderService investmentOrderService;


    /**
     * 列表
     */
    @RequestMapping("/investmentOrder/list")
    public R investmentOrderList(@RequestParam Map<String, Object> params) {
        PageUtils page = investmentOrderService.queryPage(params);
        return R.ok().put("page", page);
    }



    /**
     * 列表
     */
    @RequestMapping("/investment/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = investmentService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/investment/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        InvestmentEntity investment = investmentService.selectById(id);

        return R.ok().put("investment", investment);
    }

    /**
     * 保存
     */
    @RequestMapping("/investment/save")
    public R save(@RequestBody InvestmentEntity investment) {

        if (StringUtils.isBlank(investment.getName()) || investment.getProfit().compareTo(BigDecimal.ZERO) <= 0) {
            return R.error("不能为空");
        }

        Date dateStart = DateUtils.stringToDate(investment.getStartTimeStr(), DateUtils.DATE_PATTERN);
        investment.setStartTime(dateStart);
        Date dateEnd = DateUtils.stringToDate(investment.getEndTimeStr(), DateUtils.DATE_PATTERN);
        investment.setEndTime(dateEnd);

        investment.setStatus(1);
        investment.setCreateTime(new Date());
        investment.setUpdateTime(new Date());

        investmentService.insert(investment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/investment/update")
    public R update(@RequestBody InvestmentEntity investment) {
        ValidatorUtils.validateEntity(investment);

        if (StringUtils.isBlank(investment.getName()) || investment.getProfit().compareTo(BigDecimal.ZERO) <= 0) {
            return R.error("不能为空");
        }

        Date dateStart = DateUtils.stringToDate(investment.getStartTimeStr(), DateUtils.DATE_PATTERN);
        investment.setStartTime(dateStart);
        Date dateEnd = DateUtils.stringToDate(investment.getEndTimeStr(), DateUtils.DATE_PATTERN);
        investment.setEndTime(dateEnd);
        investment.setUpdateTime(new Date());

        investmentService.updateById(investment);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/investment/delete")
    public R delete(@RequestBody Integer[] ids) {
        investmentService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }






}
